﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;

namespace XML文件操作
{
    internal class Program
    {
        static void Main(string[] args)
        {
            //查询xml文件数据
            ReadXml();

            AddBook("3", "AI课程", "AI学习", "张三2", 88.9);
            ReadXml();


        }

        static void UpdateBook(string id, string catgory, string title, string author, double price)
        {
            Console.WriteLine("-----------------");
            Console.WriteLine("----修改数据----");

            XDocument xDoc = XDocument.Load("Books.xml");
            XElement book = xDoc.Descendants("book").FirstOrDefault(y => y.Attribute("id").Value == id);
            if (book != null)
            {
                book.Attribute("category").Value = catgory;
                book.Element("title").Value = title;
                book.Element("author").Value = author;
                book.Element("price").Value = price.ToString("0.00");

                //保存带文件里面
                xDoc.Save("Books.xml");
                Console.WriteLine("数据修改成功！！");
            }
            else
            {
                Console.WriteLine("找不到指定书籍，修改失败！！");
            }

            Console.WriteLine("-----------------");
        }
        static void DeleteBook(string id)
        {
            Console.WriteLine("-----------------");
            Console.WriteLine("----删除数据----");
            XDocument xDoc = XDocument.Load("Books.xml");
            XElement book = xDoc.Descendants("book").FirstOrDefault(y => y.Attribute("id").Value == id);
            if (book != null)
            {
                book.Remove();
                //保存带文件里面
                xDoc.Save("Books.xml");
                Console.WriteLine("数据删除成功！！");
            }
            else
            {
                Console.WriteLine("找不到指定书籍，删除失败！！");
            }
            Console.WriteLine("-----------------");
        }


        static void AddBook(string id, string catgory, string title, string author, double price)
        {
            Console.WriteLine("-----------------");
            Console.WriteLine("---插入新数据---");
            XDocument xDoc = XDocument.Load("Books.xml");
            bool exist = xDoc.Descendants("book").Any(x => x.Attribute("id").Value == id);
            if (exist)
            {
                Console.WriteLine($"当前id 为 {id} 的图书已经存在，不能重复添加！！");
                return;
            }
            XElement element = new XElement("book",
                new XAttribute("id", id),
                new XAttribute("category", catgory),
                new XElement("title", title),
                new XElement("author", author),
                new XElement("price", price)
                );
            //把新节点添加到根节点
            xDoc.Element("bookstore").Add(element);
            xDoc.Save("Books.xml");
            Console.WriteLine("新数据保存成功！！");
            Console.WriteLine("-----------------");

        }
        static void ReadXml()
        {
            //文件流形式操作
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load("Books.xml");
            //使用Linq查询xml
            XDocument xDoc = XDocument.Load("Books.xml");

            //查询1  Linq
            var book = from x in xDoc.Descendants("book")
                       select new
                       {
                           ID = x.Attribute("id").Value,
                           Category = x.Attribute("category").Value,
                           Author = x.Element("author").Value,
                           Title = x.Element("title").Value,
                           Price = x.Element("price").Value,
                       };
            Console.WriteLine("查询1：");
            foreach (var item in book)
            {
                Console.WriteLine($"  ID {item.ID}  " +
                    $"  Category {item.Category}  " +
                    $"  Title {item.Title}  " +
                    $"  Author {item.Author}  " +
                    $"  Price {item.Price}");
            }

            //查询2  文件流
            //获得xml的根节点
            XmlNode root = xmlDoc.DocumentElement;
            //遍历子节点
            Console.WriteLine("查询2：");
            foreach (XmlNode xmlNode in root.SelectNodes("book"))
            {
                //获取属性
                string id = xmlNode.Attributes["id"].Value;
                string category = xmlNode.Attributes["category"].Value;
                Console.WriteLine($"  id {id}   category {category}  ");
                //获取子节点
                foreach (XmlNode childNode in xmlNode.ChildNodes)
                {
                    if (childNode.NodeType == XmlNodeType.Element)
                    {
                        Console.WriteLine($"   {childNode.Name} : {childNode.InnerText}  ");
                    }
                }
            }
        }
    }
}
